package player

import (
	"GameServer/Common/utils"
	"GameServer/Game/errors"
	"GameServer/Game/lib"
	"GameServer/Game/plog"
	"GameServer/Game/sp"
)

func (p *Player) ShopItemsRequest(req *sp.ShopItemsRequest) (res *sp.ShopItemsResponse) {
	items := lib.GetShopItems(p.User).Items

	res = &sp.ShopItemsResponse{
		List: lib.FormatShopItemSps(items),
	}
	return
}

func (p *Player) ShopItemBuyRequest(req *sp.ShopItemBuyRequest) (res *sp.ShopItemBuyResponse, err error) {
	uProp, resGoods, boxRes, fResGoods, shopItem, err := lib.ShopBuyItem(p.User, req.Typ, req.Tid, req.Num, p.Send)
	if err != nil {
		res = &sp.ShopItemBuyResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}
	// 有成就完成，进行通知
	achLst := lib.AchievComplete(p.User)
	if len(achLst) > 0 {
		p.Send(&sp.AchievementNoticeRequest{
			Lst: achLst,
		})
	}
	plog.Log_ShopBuy(p.User, shopItem, resGoods, fResGoods, boxRes, uProp)
	p.TimelyUpdateProp(req.GetType(), uProp)
	utils.AsyncFunc(func() {
		p.UpdateProp(req.GetType(), uProp)
	})

	res = &sp.ShopItemBuyResponse{
		Res:    lib.FormatGoodsInfoSps(resGoods),
		Fres:   lib.FormatGoodsInfoSps(fResGoods),
		Item:   lib.FormatShopItemSp(shopItem),
		BoxRes: lib.FormatBoxResSps(boxRes),
	}
	return
}

// 3003 直接购买资源
func (p *Player) DirectlyBuyRequest(req *sp.DirectlyBuyRequest) (res *sp.DirectlyBuyResponse) {
	uProp, resGoods, boxRes, err := lib.DirectlyBuy("DirectlyBuyRequest", p.User, req.Tid, p.Send)
	if err != nil {
		res = &sp.DirectlyBuyResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}

	res = &sp.DirectlyBuyResponse{
		Res:    lib.FormatGoodsInfoSps(resGoods),
		BoxRes: lib.FormatBoxResSps(boxRes),
	}

	p.TimelyUpdateProp(req.GetType(), uProp)
	utils.AsyncFunc(func() {
		p.UpdateProp(req.GetType(), uProp)
	})
	return
}
